package ua.eug.airv1.map;

import ua.eug.airv1.controllers.IController;
import ua.eug.airv1.controllers.OrderController;
import ua.eug.airv1.controllers.ScreenController;
import ua.eug.airv1.utils.Config;
import ua.eug.airv1.weapon.ZWeapon;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;

import android.util.Log;

public class MapListener extends ActorGestureListener  {

	private Tile tile;
	private IController controller;
	private Boolean zoomed = false;
	
    public void touchDown (InputEvent event, float x, float y, int pointer, int button) {
    	if (pointer > 0) return; // Single finger interface
    	tile = (Tile)event.getListenerActor();
    	if (tile.getAirWeapon().equals(ZWeapon.Nobody) && tile.getGroundWeapon().equals(ZWeapon.Nobody)) {
    		controller = ScreenController.getInstance();
        	//System.out.println("touch started at (" + x + ", " + y + ") " + tile.getName() + " p " + pointer + " b " + button);
    	} else {
    		controller = OrderController.getInstance();
        	System.out.println("Order for " + tile.getName());
    	}
    	controller.touch(x, y, tile);

    }

    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
    	if (pointer == 0) controller.endTouch();
    	else zoomed = false;
    	//Log.d("Eug", "end touch "+ x + ", "+ y + " p "+ pointer+ " b " + button);
    }
    
	public boolean longPress (Actor actor, float x, float y) {
		return false;
	}

	public void pan (InputEvent event, float x, float y, float deltaX, float deltaY) {
		controller.pan(deltaX, deltaY, tile);
		//Log.d("Eug", "Pan " + x + ", " + y);
	}

	public void zoom (InputEvent event, float initialDistance, float distance) {
		if (zoomed) return;
		if (initialDistance - distance > Config.ZoomDistance) {
			ScreenController.getInstance().zoomDown();
			zoomed = true;
		}
		if (distance - initialDistance > Config.ZoomDistance) {
			ScreenController.getInstance().zoomUp();
			zoomed = true;
		}

		Log.d("Eug", "Zoom init " + initialDistance + "  dist " + distance);
	}

}
